import React from 'react';
import { connect, history } from 'umi';
import { Standard } from '@/components/Question';
import { ReplyInput } from '@/components/Reply';
import style from './index.less';
import { submitQuestion } from './service';

// eslint-disable-next-line
let auditRecordId = 0;

const AttemptAnswer = function ({ attempt, currentUser }) {
  const { questionInfo, standard } = attempt;
  const { pid } = currentUser;

  const submitHandle = async (contentList: []) => {
    const response = await submitQuestion({
      pid,
      questionId: questionInfo.questionId,
      auditRecordId: '',
      contentList,
    });

    if (response.code === 1001) {
      auditRecordId = response.data.auditRecordId; // eslint-disable-line
    }

    if (response.code === 0) {
      auditRecordId = 0;
      // message.info('提交成功')

      history.goBack();
    }

    return response;
  };

  return (
    <div className={style.content}>
      <div className={style.questionMsgBox}>
        <div
          className={style.backBtn}
          onClick={() => {
            history.goBack();
          }}
        >
          返回
        </div>
        <div>
          {questionInfo.title}
          <span className={style.questionType}>({questionInfo.type})</span>
        </div>
      </div>

      <div className={style.replyBox}>
        <Standard source={standard} />
        <div className={style.replyInput}>
          <ReplyInput
            minWordCount={standard.lowWordCount}
            maxWordCount={standard.highWordCount}
            inputList={standard.schemeContent}
            onSubmit={submitHandle}
          />
        </div>
      </div>
    </div>
  );
};

export default connect(({ attempt, user: { currentUser } }) => ({ attempt, currentUser }))(
  AttemptAnswer,
);
